/*
 *  12306 Auto Query => A javascript snippet to help you book tickets online.
 *  12306 Booking Assistant
 *  Copyright (C) 2011 Hidden
 * 
 *  12306 Auto Query => A javascript snippet to help you book tickets online.
 *  Copyright (C) 2011 Jingqin Lynn
 * 
 *  12306 Auto Login => A javascript snippet to help you auto login 12306.com.
 *  Copyright (C) 2011 Kevintop
 * 
 *  Includes jQuery
 *  Copyright 2011, John Resig
 *  Dual licensed under the MIT or GPL Version 2 licenses.
 *  http://jquery.org/license
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

// ==UserScript==  
// @name         12306 Booking Assistant
// @version		 1.4.0
// @author       zzdhidden@gmail.com
// @namespace    https://github.com/zzdhidden
// @description  12306 订票助手之(自动登录，自动查票，自动订单)
// @include      *://dynamic.12306.cn/otsweb/*
// @require	https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript== 

function InitPlugin(callback, unsafeWindow)
{
    setTimeout(function() {
            //Firefox supports
            callback(jQuery, typeof unsafeWindow === "undefined" ? window : unsafeWindow);
    }, 30);    
}

function notify(str, timeout, skipAlert) {
    if( window.webkitNotifications && window.webkitNotifications.checkPermission() == 0 ) {
        var notification = webkitNotifications.createNotification(
            "http://www.12306.cn/mormhweb/images/favicon.ico",  // icon url - can be relative
            '订票',  // notification title
            str
        );
        notification.show();
        if ( timeout ) {
            setTimeout(function() {
                    notification.cancel();
            }, timeout);
        }
        return true;
    } else {
        if( !skipAlert ) {
                alert( str );
        }
        return false;
    }
}


//withjQuery(function($, window){
InitPlugin(function($, window){
	$(document).click(function() {
		if( window.webkitNotifications && window.webkitNotifications.checkPermission() != 0 ) {
			window.webkitNotifications.requestPermission();
		}
	});

	function route(match, fn) {

		if( window.location.href.indexOf(match) != -1 ) {
			fn();
		}
	}

	route("querySingleAction.do", $.QueryInfo);
	route("myOrderAction.do?method=resign", $.QueryInfo);
	//https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=init
	route("confirmPassengerResignAction.do?method=cancelOrderToQuery", $.QueryInfo);
	////////////////
	///otsweb/order/confirmPassengerAction.do?method=confirmPassengerInfoSingle
	route("confirmPassengerAction.do?method=init", $.SubmitOrder);        
	route("loginAction.do?method=init", $.SubmitLogin);
	//route("confirmPassengerAction.do", submit);
	//route("confirmPassengerResignAction.do", submit);
});
